package quests.Tutorial;

import java.util.HashMap;

import com.l2device.gameserver.model.actor.L2Npc;
import com.l2device.gameserver.model.actor.instance.L2PcInstance;
import com.l2device.gameserver.model.quest.Quest;
import com.l2device.gameserver.model.quest.QuestState;

public class Q999_T1Tutorial extends Quest
{
	private static String qnTutorial = "Q255_Tutorial";
	private static int RECOMMENDATION_01 = 1067;
	private static int RECOMMENDATION_02 = 1068;
	private static int LEAF_OF_MOTHERTREE = 1069;
	private static int BLOOD_OF_JUNDIN = 1070;
	private static int LICENSE_OF_MINER = 1498;
	private static int VOUCHER_OF_FLAME = 1496;
	private static int SOULSHOT_NOVICE = 5789;
	private static int SPIRITSHOT_NOVICE = 5790;
	private static int BLUE_GEM = 6353;
	private static int TOKEN = 8542;
	private static int SCROLL = 8594;
	private static int DIPLOMA = 9881;

	private static class Event
	{
		public String htm;
		public int radarX;
		public int radarY;
		public int radarZ;
		public int item;
		public int classId1;
		public int gift1;
		public int count1;
		public int classId2;
		public int gift2;
		public int count2;

		public Event(String htm, int radarX, int radarY, int radarZ, int item, int classId1, int gift1, int count1, int classId2, int gift2, int count2)
		{
			this.htm = htm;
			this.radarX = radarX;
			this.radarY = radarY;
			this.radarZ = radarZ;
			this.item = item;
			this.classId1 = classId1;
			this.gift1 = gift1;
			this.count1 = count1;
			this.classId2 = classId2;
			this.gift2 = gift2;
			this.count2 = count2;
		}
	}

	private static class Talk
	{
		public int raceId;
		public String[] htmlfiles;
		public int npcTyp;
		public int item;

		public Talk(int raceId, String[] htmlfiles, int npcTyp, int item)
		{
			this.raceId = raceId;
			this.htmlfiles = htmlfiles;
			this.npcTyp = npcTyp;
			this.item = item;
		}
	}

	private static HashMap<String, Event> events = new HashMap<String, Event>();
	static
	{
		events.put("32133_02", new Event("32133-03.htm", -119692, 44504, 380, DIPLOMA, 0x7b, SOULSHOT_NOVICE, 200, 0x7c, SOULSHOT_NOVICE, 200));
		events.put("30008_02", new Event("30008-03.htm", 0, 0, 0, RECOMMENDATION_01, 0x00, SOULSHOT_NOVICE, 200, 0x00, 0, 0));
		events.put("30008_04", new Event("30008-04.htm", -84058, 243239, -3730, 0, 0x00, 0, 0, 0, 0, 0));
		events.put("30017_02", new Event("30017-03.htm", 0, 0, 0, RECOMMENDATION_02, 0x0a, SPIRITSHOT_NOVICE, 100, 0x00, 0, 0));
		events.put("30017_04", new Event("30017-04.htm", -84058, 243239, -3730, 0, 0x0a, 0, 0, 0x00, 0, 0));
		events.put("30370_02", new Event("30370-03.htm", 0, 0, 0, LEAF_OF_MOTHERTREE, 0x19, SPIRITSHOT_NOVICE, 100, 0x12, SOULSHOT_NOVICE, 200));
		events.put("30370_04", new Event("30370-04.htm", 45491, 48359, -3086, 0, 0x19, 0, 0, 0x12, 0, 0));
		events.put("30129_02", new Event("30129-03.htm", 0, 0, 0, BLOOD_OF_JUNDIN, 0x26, SPIRITSHOT_NOVICE, 100, 0x1f, SOULSHOT_NOVICE, 200));
		events.put("30129_04", new Event("30129-04.htm", 12116, 16666, -4610, 0, 0x26, 0, 0, 0x1f, 0, 0));
		events.put("30528_02", new Event("30528-03.htm", 0, 0, 0, LICENSE_OF_MINER, 0x35, SOULSHOT_NOVICE, 200, 0x00, 0, 0));
		events.put("30528_04", new Event("30528-04.htm", 115642, -178046, -941, 0, 0x35, 0, 0, 0x00, 0, 0));
		events.put("30573_02", new Event("30573-03.htm", 0, 0, 0, VOUCHER_OF_FLAME, 0x31, SPIRITSHOT_NOVICE, 100, 0x2c, SOULSHOT_NOVICE, 200));
		events.put("30573_04", new Event("30573-04.htm", -45067, -113549, -235, 0, 0x31, 0, 0, 0x2c, 0, 0));
	}

	private static HashMap<Integer, Talk> talks = new HashMap<Integer, Talk>();
	static
	{
		talks.put(30017, new Talk(0, new String[] { "30017-01.htm", "30017-02.htm", "30017-04.htm" }, 0, 0));
		talks.put(30008, new Talk(0, new String[] { "30008-01.htm", "30008-02.htm", "30008-04.htm" }, 0, 0));
		talks.put(30370, new Talk(1, new String[] { "30370-01.htm", "30370-02.htm", "30370-04.htm" }, 0, 0));
		talks.put(30129, new Talk(2, new String[] { "30129-01.htm", "30129-02.htm", "30129-04.htm" }, 0, 0));
		talks.put(30573, new Talk(3, new String[] { "30573-01.htm", "30573-02.htm", "30573-04.htm" }, 0, 0));
		talks.put(30528, new Talk(4, new String[] { "30528-01.htm", "30528-02.htm", "30528-04.htm" }, 0, 0));
		talks.put(30018, new Talk(0, new String[] { "30131-01.htm", "", "30019-03a.htm", "30019-04.htm", }, 1, RECOMMENDATION_02));
		talks.put(30019, new Talk(0, new String[] { "30131-01.htm", "", "30019-03a.htm", "30019-04.htm", }, 1, RECOMMENDATION_02));
		talks.put(30020, new Talk(0, new String[] { "30131-01.htm", "", "30019-03a.htm", "30019-04.htm", }, 1, RECOMMENDATION_02));
		talks.put(30021, new Talk(0, new String[] { "30131-01.htm", "", "30019-03a.htm", "30019-04.htm", }, 1, RECOMMENDATION_02));
		talks.put(30009, new Talk(0, new String[] { "30530-01.htm", "30009-03.htm", "", "30009-04.htm", }, 1, RECOMMENDATION_01));
		talks.put(30011, new Talk(0, new String[] { "30530-01.htm", "30009-03.htm", "", "30009-04.htm", }, 1, RECOMMENDATION_01));
		talks.put(30012, new Talk(0, new String[] { "30530-01.htm", "30009-03.htm", "", "30009-04.htm", }, 1, RECOMMENDATION_01));
		talks.put(30056, new Talk(0, new String[] { "30530-01.htm", "30009-03.htm", "", "30009-04.htm", }, 1, RECOMMENDATION_01));
		talks.put(30400, new Talk(1, new String[] { "30131-01.htm", "30400-03.htm", "30400-03a.htm", "30400-04.htm", }, 1, LEAF_OF_MOTHERTREE));
		talks.put(30401, new Talk(1, new String[] { "30131-01.htm", "30400-03.htm", "30400-03a.htm", "30400-04.htm", }, 1, LEAF_OF_MOTHERTREE));
		talks.put(30402, new Talk(1, new String[] { "30131-01.htm", "30400-03.htm", "30400-03a.htm", "30400-04.htm", }, 1, LEAF_OF_MOTHERTREE));
		talks.put(30403, new Talk(1, new String[] { "30131-01.htm", "30400-03.htm", "30400-03a.htm", "30400-04.htm", }, 1, LEAF_OF_MOTHERTREE));
		talks.put(30131, new Talk(2, new String[] { "30131-01.htm", "30131-03.htm", "30131-03a.htm", "30131-04.htm", }, 1, BLOOD_OF_JUNDIN));
		talks.put(30404, new Talk(2, new String[] { "30131-01.htm", "30131-03.htm", "30131-03a.htm", "30131-04.htm", }, 1, BLOOD_OF_JUNDIN));
		talks.put(30574, new Talk(3, new String[] { "30575-01.htm", "30575-03.htm", "30575-03a.htm", "30575-04.htm", }, 1, VOUCHER_OF_FLAME));
		talks.put(30575, new Talk(3, new String[] { "30575-01.htm", "30575-03.htm", "30575-03a.htm", "30575-04.htm", }, 1, VOUCHER_OF_FLAME));
		talks.put(30530, new Talk(4, new String[] { "30530-01.htm", "30530-03.htm", "", "30530-04.htm", }, 1, LICENSE_OF_MINER));
		talks.put(30598, new Talk(0, null, 0, 0));
		talks.put(30599, new Talk(0, null, 0, 0));
		talks.put(30600, new Talk(0, null, 0, 0));
		talks.put(30601, new Talk(0, null, 0, 0));
		talks.put(30602, new Talk(0, null, 0, 0));
	}

	public Q999_T1Tutorial(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(new int[] { 30009, 30019, 30131, 30575, 30530, 30400, 30401, 30402, 30403, 30404, 32133, 32134 });
		addTalkId(new int[] { 30008, 30017, 30018, 30020, 30021, 30011, 30012, 30056, 30129, 30370, 30528, 30573, 30574 });
		addFirstTalkId(new int[] { 30600, 30601, 30602, 30598, 30599});
		addKillId(18342);
		addKillId(20001);
	}

	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState("Q999_T1Tutorial");
		QuestState qs = st.getPlayer().getQuestState(qnTutorial);
		if(qs == null)
			return null;
		String htmltext;
		int Ex = qs.getInt("Ex");
		int classId = player.getClassId().getId();
		if(event.equalsIgnoreCase("TimerEx_NewbieHelper"))
		{
			if(Ex == 0)
			{
				if(player.getClassId().isMage())
					st.playTutorialVoice("tutorial_voice_009b");
				else
					st.playTutorialVoice("tutorial_voice_009a");
				qs.set("Ex", "1");
			}
			else if(Ex == 3)
			{
				st.playTutorialVoice("tutorial_voice_010a");
				qs.set("Ex", "4");
			}
			return null;
		}
		else if(event.equalsIgnoreCase("TimerEx_GrandMaster"))
		{
			if(Ex >= 4)
			{
				st.showQuestionMark(7);
				st.playTutorialVoice("tutorial_voice_025");
			}
			return null;
		}
		else if(event.equalsIgnoreCase("isle"))
		{
			st.addRadar(-119692, 44504, 380);
			player.teleToLocation(-120050, 44500, 360, 50);
			htmltext = "<html><body><br>Go to the <font color=\"LEVEL\">Isle of Souls</font> and meet the <font color=\"LEVEL\">Newbie Guide</font> there to learn a number of important tips. He will also give you an item to assist your development. <br>Follow the direction arrow above your head and it will lead you to the Newbie Guide. Good luck!</body></html>";
		}
		else
		{
			Event e = events.get(event);
			if(e.radarX != 0)
				st.addRadar(e.radarX, e.radarY, e.radarZ);
			htmltext = e.htm;
			if(st.getQuestItemsCount(e.item) > 0)
			{
				st.rewardExpAndSp(0, 50);
				startQuestTimer("TimerEx_GrandMaster", 60000, null, st.getPlayer(), true);
				st.takeItems(e.item, 1);
				if(Ex <= 3)
					qs.set("Ex", "4");
				if(classId == e.classId1)
				{
					st.giveItems(e.gift1, e.count1);
					if(e.gift1 == SPIRITSHOT_NOVICE)
						st.playTutorialVoice("tutorial_voice_027");
					else
						st.playTutorialVoice("tutorial_voice_026");
				}
				else if(classId == e.classId2)
					if(e.gift2 != 0)
					{
						st.giveItems(e.gift2, e.count2);
						st.playTutorialVoice("tutorial_voice_026");
					}
			}
		}
		return htmltext;
	}

	@Override
	public String onFirstTalk(L2Npc npc, L2PcInstance player)
	{
		int npcId = npc.getNpcId();
		if(npcId == 30600 || npcId == 30601 || npcId == 30602 || npcId == 30598 || npcId == 30599)
		{
			QuestState qs = player.getQuestState(qnTutorial);
			if(qs == null || qs.isCompleted())
				return null;
			if(player.getClassId().isMage())
			{
				qs.playTutorialVoice("tutorial_voice_027");
				qs.giveItems(SPIRITSHOT_NOVICE, 100);
			}
			else
			{
				qs.playTutorialVoice("tutorial_voice_026");
				qs.giveItems(SOULSHOT_NOVICE, 200);
			}
			qs.giveItems(TOKEN, 12);
			qs.giveItems(SCROLL, 2);
			qs.exitQuest(false);
		}
		return null;
	}

	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState qs = player.getQuestState(qnTutorial);
		QuestState st = player.getQuestState("Q999_T1Tutorial");
		if(qs == null)
			return null;

		String htmltext = "noquest";
		int npcId = npc.getNpcId();
		int Ex = qs.getInt("Ex");
		int step = st.getInt("step");
		int level = player.getLevel();
		boolean isMage = player.getClassId().isMage();

		Talk t = talks.get(npcId);
		if(t == null)
			return null;

		if(level >= 10 && t.npcTyp == 1)
			htmltext = "30575-05.htm";
		else if(!(npcId == 30600 || npcId == 30601 || npcId == 30602 || npcId == 30598 || npcId == 30599 || npcId == 32135))
		{
			if(player.getRace().ordinal() == t.raceId)
			{
				htmltext = t.htmlfiles[0];
				if(t.npcTyp == 1)
				{
					if(step == 0 && Ex < 0)
					{
						qs.set("Ex", "0");
						startQuestTimer("TimerEx_NewbieHelper", 30000, npc, st.getPlayer(), true);
						if(isMage)
						{
							st.set("step", "1");
							st.setState(STATE_STARTED);
						}
						else
						{
							htmltext = "30530-01.htm";
							st.set("step", "1");
							st.setState(STATE_STARTED);
						}
					}
					else if(step == 1 && st.getQuestItemsCount(t.item) == 0 && Ex <= 2)
					{
						if(st.getQuestItemsCount(BLUE_GEM) > 0)
						{
							st.takeItems(BLUE_GEM, st.getQuestItemsCount(BLUE_GEM));
							st.giveItems(t.item, 1);
							st.set("step", "2");
							qs.set("Ex", "3");
							startQuestTimer("TimerEx_NewbieHelper", 30000, npc, st.getPlayer(), true);
							qs.set("ucMemo", "3");
							if(isMage)
							{
								st.playTutorialVoice("tutorial_voice_027");
								st.giveItems(SPIRITSHOT_NOVICE, 100);
								htmltext = t.htmlfiles[2];
								if(htmltext.isEmpty())
									htmltext = "<html><body>I am sorry. I only help warriors. Please go to another Newbie Helper who may assist you.</body></html>";
							}
							else
							{
								st.playTutorialVoice("tutorial_voice_026");
								st.giveItems(SOULSHOT_NOVICE, 200);
								htmltext = t.htmlfiles[1];
								if(htmltext.isEmpty())
									htmltext = "<html><body>I am sorry. I only help mystics. Please go to another Newbie Helper who may assist you.</body></html>";
							}
						}
						else if(isMage)
						{
							htmltext = "30131-02.htm";
							if(player.getRace().ordinal() == 3)
								htmltext = "30575-02.htm";
						}
						else
							htmltext = "30530-02.htm";
					}
					else if(step == 2)
						htmltext = t.htmlfiles[3];
				}
				else if(t.npcTyp == 0)
					if(step == 1)
						htmltext = t.htmlfiles[0];
					else if(step == 2)
						htmltext = t.htmlfiles[1];
					else if(step == 3)
						htmltext = t.htmlfiles[2];
			}
		}
		else
			htmltext = "<html><body>You are too experienced now.</body></html>";

		return htmltext;
	}

	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
			return null;
		QuestState qs = st.getPlayer().getQuestState(qnTutorial);
		if(qs == null)
			return null;
		int Ex = qs.getInt("Ex");
		if(Ex <= 1)
		{
			st.playTutorialVoice("tutorial_voice_011");
			st.showQuestionMark(3);
			qs.set("Ex", "2");
		}
		if(Ex <= 2 && st.getQuestItemsCount(BLUE_GEM) < 1)
		{
			st.dropItems(BLUE_GEM, 1, 1, 500000);
			st.playSound(QuestState.SOUND_FINISH);
		}
		return null;
	}

	public static void main(String[] args)
	{
		new Q999_T1Tutorial(999, "Q999_T1Tutorial", "Tutorial");
	}
}